<template>
  <div>
    <el-dialog :title="title" :visible.sync="visibleOfDialog">
      <el-form ref="form" :model="formData" size="mini" label-width="80px">
        <el-row>
          <el-col :span='12'>
            <el-form-item label="父级ID">
              <el-input-number v-model="formData.pid" :min="1" :max="10" label="请输入父级ID"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span='12'>
            <el-form-item label="规则标题">
              <el-input v-model="formData.text" placeholder="请输入规则标题" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span='12'>
            <el-form-item label="规则路径">
              <el-input v-model="formData.link" placeholder="请输入规则路径" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span='12'>
            <el-form-item label="图标">
              <el-upload class="avatar-uploader" action="https://jsonplaceholder.typicode.com/posts/"
                :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span='12'>
            <el-form-item label="扩展数据">
              <el-input v-model="formData.extra" placeholder="请输入扩展数据" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span='12'>
            <el-form-item label="排序权重">
              <el-input-number v-model="formData.sort" :min="1" :max="10" label="请输入排序权重"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span='12'>
            <el-form-item label="模块标志">
              <el-input v-model="formData.app_flag" placeholder="请输入模块标志" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span='12'>
            <el-form-item label="状态">
              <el-input-number v-model="formData.status" :min="1" :max="10" label="请输入状态"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="visibleOfDialog = false">取消</el-button>
        <el-button type="primary" :loading="loadingOfSubmit" @click="onSubmit()">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
/**
 * 编辑权限规则
 */
export default {
  name: "RulesEdit",
  emits: ["submit"],
  data() {
    return {
      visibleOfDialog: false,
      loadingOfSubmit: false,
      id: 0,
      title: "编辑",
      formData: {},
    };
  },
  methods: {
    // 打开对话框（用于外部传参式调用）
    open(title = "编辑", id = 0) {
      this.title = title;
      this.id = id;
      this.visibleOfDialog = true;
      this.loadData();
    },
    // 加载详情数据
    loadData() {
      console.log('[加载详情数据] 查询参数= ', { id: this.id });
      this.vuiAjax
        .put(this.apiUrls.rulesRead, { id: this.id })
        .then(ret => {
          this.loadingOfSubmit = false;
          console.log("[加载详情数据] 返回结果= ", ret);
          if (!ret.success) {
            this.$message.error(ret.message);
            return;
          }
          this.dataOfForm = ret.data;
        })
        .catch(msgInfo => {
          this.$message.error(msgInfo.msg);
        });
    },
    // 提交表单数据
    onSubmit() {
      console.log("[提交表单数据] 表单数据= ", this.formData);
      this.loadingOfSubmit = true;
      if (this.id > 0) {
        this.vuiAjax
          .put(this.apiUrls.rulesUpdate, { params: this.dataOfForm })
          .then(ret => {
            console.log("[提交表单数据-更新模式] 返回结果= ", ret);
            this.loadingOfSubmit = false;
            if (!ret.success) {
              this.$message.error(ret.message);
              return;
            }
            this.id = ret.data;
          })
          .catch(msgInfo => {
            this.loadingOfSubmit = false;
            this.$message.error(msgInfo.msg);
          });
        return;
      }
      this.vuiAjax
        .post(this.apiUrls.rulesUpdate, this.dataOfForm)
        .then(ret => {
          console.log("[提交表单数据-新建模式] 返回结果= ", ret);
          this.loadingOfSubmit = false;
          if (!ret.success) {
            this.$message.error(ret.message);
            return;
          }
          this.id = ret.data;
        })
        .catch(msgInfo => {
          this.loadingOfSubmit = false;
          this.$message.error(msgInfo.msg);
        });
    },
  },
};
</script>